//
//  UserViewController.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "UserViewController.h"
#import "FitHeader.h"
#import "BaseLabel.h"
#import "CacheClean.h"
#import "NightTableViewCell.h"
#import "Model.h"
#import "BaseView.h"
#import "AboutUsViewController.h"
#import "AdviceBackViewController.h"


@interface UserViewController ()<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSArray *settingArray;
@property(nonatomic, strong)UISwitch *daySwitch;
@property(nonatomic, strong)BaseLabel *clearCacheLabel;
@property(nonatomic, strong)UISwitch *mySwitch;
@property(nonatomic, strong)BaseView *backView;
@property(nonatomic, strong)NSArray *logoArray;
@property(nonatomic, assign)NSInteger flag;
@end

@implementation UserViewController

-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:0.84 green:0.23 blue:0 alpha:1]];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[Model shareInstance].TitleColor, NSForegroundColorAttributeName, nil]];
    self.view.backgroundColor = [Model shareInstance].ViewColor;
    _tableView.backgroundColor = [Model shareInstance].ViewColor;
    [_tableView reloadData];
}

-(void)loadView{
    [super loadView];
    self.logoArray = @[@"iconfont-yejianmoshi", @"iconfont-qingchuhuancun", @"iconfont-yijianfankui", @"iconfont-guanyu"];
    self.settingArray = [NSArray arrayWithObjects:@"夜间模式", @"清除缓存", @"意见反馈", @"关于我们", nil];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.bounces = NO;
    //去掉多余的横线
    _tableView.tableFooterView = [[BaseView alloc]init];
    [self.view addSubview:_tableView];
    
    _flag = [[NSUserDefaults standardUserDefaults] integerForKey:@"night"];

    
    //清理缓存
//    self.clearCacheLabel = [[BaseLabel alloc]initWithFrame:CGRectMake(300 * FITWidth, 58 * FITHeight, 70 * FITWidth, 30 * FITHeight)];
//    _clearCacheLabel.text = @"清理缓存";
//    [_clearCacheLabel setFontSize:15];
//    _clearCacheLabel.textAlignment = 1;
//    [self.view addSubview:_clearCacheLabel];
    
 
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"设置";
    self.navigationController.navigationBar.translucent = NO;
    // Do any additional setup after loading the view.
}

-(void)switchAction{
    if (_flag == 1) {
        [[Model shareInstance] changeColor:_flag - 1];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:[NSNumber numberWithInteger:_flag]];
        [[NSUserDefaults standardUserDefaults] setInteger:_flag - 1 forKey:@"night"];
        [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[Model shareInstance].TitleColor, NSForegroundColorAttributeName, nil]];
        _tableView.backgroundColor = [Model shareInstance].ViewColor;
        [_tableView reloadData];
        _flag = 0;
    }else if (_flag == 0){
        [[Model shareInstance] changeColor:_flag + 1];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:[NSNumber numberWithInteger:_flag]];
        [[NSUserDefaults standardUserDefaults] setInteger:_flag + 1 forKey:@"night"];
        [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[Model shareInstance].TitleColor, NSForegroundColorAttributeName, nil]];
        _tableView.backgroundColor = [Model shareInstance].ViewColor;
        [_tableView reloadData];
        _flag = 1;
    }
}

#pragma mark - cell的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _settingArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70 * FITHeight;
}

#pragma mark - cell的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 1) {
        static NSString *identifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        }
        cell.selectionStyle = 0;
        UIImage *image = [UIImage imageNamed:_logoArray[indexPath.row]];
        cell.imageView.image = image;
        cell.textLabel.text = @"清理缓存";
        cell.detailTextLabel.textColor = [UIColor colorWithRed:0.78 green:0.78 blue:0.8 alpha:1];
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
        NSString *cachePath = [path stringByAppendingPathComponent:@"Caches"];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2fM", [CacheClean folderSizeAtpath:cachePath]];
        cell.backgroundColor = [Model shareInstance].ViewColor;
        cell.textLabel.textColor = [Model shareInstance].TitleColor;
        return cell;
    }else if (indexPath.row == 0){
        static NSString *identifier1 = @"cell1";
        NightTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier1];
        if (!cell) {
            cell = [[NightTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier1];
        }
        UIImage *image = [UIImage imageNamed:_logoArray[indexPath.row]];
        cell.imageView.image = image;
        if (_flag == 0) {
            [cell.rightSwitch setOn:NO];
        }else
            [cell.rightSwitch setOn:YES];
        [cell.rightSwitch addTarget:self action:@selector(switchAction) forControlEvents:UIControlEventValueChanged];
        cell.leftLabel.textColor = [Model shareInstance].TitleColor;
        cell.backgroundColor = [Model shareInstance].ViewColor;
        return cell;

    }else{
        static NSString *identifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.selectionStyle = 0;
        UIImage *image = [UIImage imageNamed:_logoArray[indexPath.row]];
        cell.backgroundColor = [Model shareInstance].ViewColor;
        cell.imageView.image = image;
        cell.textLabel.textColor = [Model shareInstance].TitleColor;
        cell.textLabel.text = _settingArray[indexPath.row];
        return cell;

    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 1) {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
        NSString *cachePath = [path stringByAppendingPathComponent:@"Caches"];
        [CacheClean cleanSizeAtPath:cachePath];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2fM", [CacheClean folderSizeAtpath:cachePath]];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"缓存清理成功" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *alertAction2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //            [alert removeFromParentViewController];
        }];
        [alert addAction:alertAction2];
        [self presentViewController:alert animated:YES completion:nil];
    }else if (indexPath.row == 3) {
        AboutUsViewController *about = [[AboutUsViewController alloc]init];
        [self.navigationController pushViewController:about animated:YES];
    }else if (indexPath.row == 2){
        AdviceBackViewController *av = [[AdviceBackViewController alloc]init];
        [self.navigationController pushViewController:av animated:YES];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
